<?php

class DefaultController {

    public function invoke() {

        $dir = dirname(__FILE__);
        $viewFile = $dir . '/views/default.html';
        $xtemplate = new XTemplate($viewFile);


        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }



        if ($action == 'logout') {

            Utility::destroySession();
//            Utility::redirect('.');
            echo'OK';
            exit();
        }


        $router_file = '';

        $com = '';
        if (isset($_GET['com'])) {
            $com = $_GET['com'];
        }


        switch ($com) {
            case 'com_danhmuc':
                $router_file = './components/com_danhmuc/index.php';
                break;
            case 'com_hanghoa':
                $router_file = './components/com_hanghoa/index.php';
                break;
            case 'com_dm_loaihoahong':
                $router_file = './components/com_dm_loaihoahong/index.php';
                break;
           case 'com_dm_truong':
                $router_file = './components/com_truong/index.php';
                break;
            case 'com_bep':
                $router_file = './components/com_bep/index.php';
                break;
            case 'com_logic_chi':
                $router_file = './components/com_logic_chi/index.php';
                break;
           case 'com_logic_thu':
                $router_file = './components/com_logic_thu/index.php';
                break;
           case 'com_logic_kiemke':
                $router_file = './components/com_logic_kiemke/index.php';
                break;
            case 'com_logic_nhap':
                $router_file = './components/com_logic_nhap/index.php';
                break;
            
            case 'com_logic_xuat':
                $router_file = './components/com_logic_xuat/index.php';
                break;
             case 'com_thongke_chi':
                $router_file = './components/com_thongke_chi/index.php';
                break;
            
            case 'com_thongke_thu':
                $router_file = './components/com_thongke_thu/index.php';
                break;
            case 'com_thongke_kiemke':
                $router_file = './components/com_thongke_kiemke/index.php';
                break;
            case 'com_thongke_loinhuan':
                $router_file = './components/com_thongke_loinhuan/index.php';
                break;
            default:
                $router_file = './components/com_danhmuc/index.php';
                break;
        }
        $sysGroups = $this->getAllSysGroup();
        if ($sysGroups != null) {
            foreach ($sysGroups as $sysGroup) {
                $sysFunctions = $this->getAllFunction($sysGroup->getSFG_ID());
                if ($sysFunctions != null) {
                    foreach ($sysFunctions as $sysFunction) {
                        $xtemplate->assign('SF_NAME', $sysFunction->getSF_NAME());
                        $xtemplate->assign('SF_URL', $sysFunction->getSF_URL());
                        $xtemplate->parse('main.DANH_MUC.CHUC_NANG');
                    }
                }
                $xtemplate->assign('SFG_NAME', $sysGroup->getSFG_NAME());
                $xtemplate->parse('main.DANH_MUC');
            }
        }
        $content = Utility::assignFileContentToVariable($router_file);        
        $xtemplate->assign('ROUTER_FILE', $content);
        $xtemplate->parse('main');
        $xtemplate->out('main');
    }

    private function getAllSysGroup() {
        $db = new Database();
        try {
            $obj = new Sys_func_group();
            $result = $obj->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getAllFunction($groupID) {
        $db = new Database();
        try {
            $obj = new Sys_func();
            $obj->setSFG_ID($groupID);
            $result = $obj->getListByGroupID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
